import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtCore import QCoreApplication, QObject, Qt, pyqtSignal, pyqtSlot

class Example(QWidget):
    help_signal = pyqtSignal(str)  # 信号必须在类中定义
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 300, 400)

        btn = QPushButton('Button', self)
        btn.resize(btn.sizeHint())
        btn.move(50,50)

        btn.clicked.connect(QCoreApplication.instance().quit)
        self.help_signal.connect(self.show_help)  # 将help_signal信号绑定响应函数
        self.show()

    def show_help(self, msg):
        QMessageBox.information(self, "", msg)

    def keyPressEvent(self, event):  # 重写键盘事件，在按键事件发生时自动调用
        if event.key() == Qt.Key_Escape:
            print("Get Escape Key, Exiting.")
            self.close()
        elif event.key() == Qt.Key_F1:
            self.help_signal.emit("Hello, This is help message.")  # 利用help_signal对象发送信号


if __name__ == '__main__':

    app = QApplication(sys.argv)
    win = Example()
    sys.exit(app.exec_())
